package com.wiscamp.ninechapters.challenges.application;

import com.wiscamp.ninechapters.challenges.domain.models.InviteHelp;
import com.wiscamp.ninechapters.challenges.domain.models.UserGroup;
import com.wiscamp.ninechapters.challenges.domain.services.GroupsService;
import com.wiscamp.ninechapters.challenges.domain.services.PointsService;
import cube.ddd.aspect.AopServiceException;
import com.wiscamp.ninechapters.ddd.ApplicationService;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Service
public class ChallengesApplicationService implements ApplicationService {

    @Autowired
    private GroupsService groupsService;

    @Autowired
    private PointsService pointsService;

/*    @Autowired
    private AssignmentTaskService assignmentTaskService;

    @Autowired
    private SheetService sheetService;*/

    /**
     * Creates a user group
     *
     * @param group group instance
     * @return group instance
     */
    @Transactional
    @AopServiceException
    public UserGroup createUserGroup(@NotNull UserGroup group) {
        var userGroup = groupsService.createUserGroup(group);

        // todo 通过触发创建用户组领域事件来更新团队点数，进一步解耦
        if (Objects.nonNull(userGroup)) {
            pointsService.updateGroupPoints(group.getManagerId());
            return userGroup;
        }
        return null;
    }

    /**
     * Creates an invitation help
     *
     * @param inviteHelp
     * @return
     */
    @Transactional
    @AopServiceException
    public InviteHelp createInviteHelp(@NotNull InviteHelp inviteHelp) {
        var newInviteHelp = groupsService.createInviteHelp(inviteHelp);

        // todo 通过触发邀请助力领域事件来为双方增加生命值，进一步解耦
        // 邀请后各自增加生命值
        pointsService.addHeart(inviteHelp.getUserId());
        pointsService.addHeart(inviteHelp.getSupporterId());
        return newInviteHelp;
    }

/*    public ChallengeReportDTO getBattleReport(@Min(0) long taskId) {
        var taskView = assignmentTaskService.getAssignmentTaskViewById(taskId);
        var report = sheetService.getReportByTask(taskId);
        if (Objects.nonNull(taskView) && Objects.nonNull(report)) {
            var dto = pointsService.getBattleReport(taskView, report);
            calculatePersonalPoints(report.getUserId(), dto.getTotalGainPoints(), dto.getTotalGainHearts(), dto.getGainHurt());
            return dto;
        }
        return null;
    }*/

    public void calculatePersonalPoints(@Min(0) long userId, int pointQty, int heartQty, int hurt) {
        var userPoints = pointsService.calculatePersonalPoints(userId, pointQty, heartQty, hurt);
        // todo 通过触发计算个人点数领域事件来冲计算组队积分排名，进一步解耦
        // 如果用户隶属某个组队，重计算组队积分和排名
        if (Objects.nonNull(userPoints) && userPoints.isHasGroup()) {
            groupsService.calculateGroupPointsAndRank(userPoints.getUserId(), pointQty);
        }
    }


}
